package com.fhzn.boss.web.converter;

import com.fhzn.boss.entity.Resource;
import com.fhzn.boss.tools.DateTimeUtils;
import com.fhzn.boss.web.vo.ResourceExportVO;
import com.fhzn.boss.web.vo.ResourceVO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/20
 */
public interface ResourceConverter {

    /**
     * 资源实体转换
     *
     * @param resource 请求参数
     * @return 返回参数
     */
    static ResourceVO fromResource(Resource resource) {
        ResourceVO vo = new ResourceVO();
        if (resource != null) {
            vo.setStatus(resource.getStatus());
            vo.setId(resource.getId());
            vo.setAppCode(resource.getAppCode());
            vo.setCode(resource.getCode());
            vo.setSort(resource.getSort());
            vo.setComment(resource.getComment());
            vo.setIcon(resource.getIcon());
            vo.setName(resource.getName());
            vo.setLevel(resource.getLevel());
            vo.setParentCode(resource.getParentCode());
            vo.setResourceType(resource.getResourceType());
            vo.setResourceUrl(resource.getResourceUrl());
            vo.setResourceExt(resource.getResourceExt());
            vo.setModifier(resource.getModifier());
            if (resource.getUpdatedTime() != null) {
                vo.setUpdatedTime(DateTimeUtils.format(resource.getUpdatedTime()));
            }
            vo.setAppCode(resource.getAppCode());
            vo.setIcon(resource.getIcon());
        }
        return vo;
    }

    /**
     * 资源实体转换
     *
     * @param resource 请求参数
     * @return 返回参数
     */
    static ResourceExportVO fromExportResource(Resource resource) {
        ResourceExportVO vo = new ResourceExportVO();
        if (resource != null) {
            vo.setStatus(resource.getStatus());
            vo.setId(resource.getId());
            vo.setAppCode(resource.getAppCode());
            vo.setCode(resource.getCode());
            vo.setSort(resource.getSort());
            vo.setComment(resource.getComment());
            vo.setName(resource.getName());
            vo.setLevel(resource.getLevel());
            vo.setResourceType(resource.getResourceType());
            vo.setResourceUrl(resource.getResourceUrl());
            vo.setModifier(resource.getModifier());
            vo.setChildren(resource.getChildren());
            if (resource.getUpdatedTime() != null) {
                vo.setUpdatedTime(DateTimeUtils.format(resource.getUpdatedTime()));
            }
            vo.setAppCode(resource.getAppCode());
            vo.setIcon(resource.getIcon());
        }
        return vo;
    }

    /**
     * 资源实体转换
     *
     * @param resources 请求参数
     * @return 返回参数
     */
    static List<ResourceExportVO> convertResourceToVoList(List<Resource> resources) {
        if (CollectionUtils.isEmpty(resources)) {
            return Lists.newArrayList();
        }
        return resources.stream().map(ResourceConverter::fromExportResource).collect(Collectors.toList());
    }

    /**
     * 资源实体转换
     *
     * @param resources 请求参数
     * @return 返回参数
     */
    static List<ResourceVO> convertToVoList(List<Resource> resources) {
        if (CollectionUtils.isEmpty(resources)) {
            return Lists.newArrayList();
        }
        return resources.stream().map(ResourceConverter::fromResource).collect(Collectors.toList());
    }

    /**
     * 资源实体转换
     *
     * @param resourceVO 请求参数
     * @return 返回参数
     */
    static Resource fromResourceVO(ResourceVO resourceVO) {
        Resource resource = new Resource();
        if (resourceVO != null) {
            resource.setAppCode(resourceVO.getAppCode());
            resource.setCode(resourceVO.getCode());
            resource.setSort(resourceVO.getSort());
            resource.setResourceExt(resourceVO.getResourceExt());
            resource.setIcon(resourceVO.getIcon());
            resource.setComment(resourceVO.getComment());
            resource.setName(resourceVO.getName());
            resource.setLevel(resourceVO.getLevel());
            resource.setParentCode(resourceVO.getParentCode());
            resource.setResourceType(resourceVO.getResourceType());
            resource.setResourceUrl(resourceVO.getResourceUrl());
            resource.setStatus(resourceVO.getStatus());
            resource.setModifier(resourceVO.getModifier());
            resource.setChildren(resourceVO.getChildren());
            return resource;
        }
        return resource;
    }
}
